package com.forum.impl.confession;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.common.entity.ApiResult;
import com.common.utils.LocalContext;
import com.common.vo.ConfessionWallCommentReplyVo;
import com.forum.mapper.confession.ConfessionWallCommentReplyMapper;
import com.forum.mapper.forum.ForumUserMapper;
import com.forum.param.ConfessionCommentReplyQueryParam;
import com.forum.pojo.confession.ConfessionWallCommentReply;
import com.forum.pojo.forum.ForumUser;
import com.forum.service.confession.ConfessionWallCommentReplyService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Tsing
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ConfessionWallCommentReplyServiceImpl extends ServiceImpl<ConfessionWallCommentReplyMapper, ConfessionWallCommentReply> implements ConfessionWallCommentReplyService {
    private final ConfessionWallCommentReplyMapper replyMapper;
    private final ForumUserMapper userMapper;

    @Override
    public ApiResult<Map<String,Object>> getRepliesByComment(ConfessionCommentReplyQueryParam param) {
        Map<String,Object> map = new HashMap<>(16);
        Page<ConfessionWallCommentReply> page = new Page<>();
        page.setCurrent(param.getPage()).setSize(param.getSize());
        LambdaQueryWrapper<ConfessionWallCommentReply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ConfessionWallCommentReply::getCommentId,param.getCommentId());
        Page<ConfessionWallCommentReply> page1 = replyMapper.selectPage(page, queryWrapper);
        List<ConfessionWallCommentReply> records = page1.getRecords();
        List<ConfessionWallCommentReplyVo> replyVoList = records.stream().map(reply -> {
            ConfessionWallCommentReplyVo replyVo = new ConfessionWallCommentReplyVo();
            BeanUtils.copyProperties(reply, replyVo);
            //获取发布者头像和昵称
            ForumUser user = userMapper.selectById(reply.getPublisherId());
            replyVo.setPublisherAvatar(user.getAvatar());
            replyVo.setPublisherName(user.getNickname());
            //获取被回复的用户的昵称
            ForumUser parentUser = userMapper.selectById(reply.getParentUserId());
            replyVo.setParentUserName(parentUser.getNickname());
            return replyVo;
        }).collect(Collectors.toList());
        map.put("total",page1.getTotal());
        map.put("list",replyVoList);
        return ApiResult.success(map);
    }

    @Override
    public ApiResult<String> sendReply(ConfessionWallCommentReply reply) {
        Integer currentUserId = LocalContext.getCurrentUserId();
        reply.setPublisherId(currentUserId);
        replyMapper.insert(reply);
        return ApiResult.success();
    }
}
